package com.gko100.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Administrator;
import com.gko100.util.MD5;
import com.opensymphony.xwork2.ActionContext;

public class AdminAction extends AdminBaseAction {
	
	
	private Administrator admin;
	
	public Administrator getAdmin() {
		return admin;
	}
	public void setAdmin(Administrator admin) {
		this.admin = admin;
	}
	
	private HttpServletRequest request;
	
	private List<Administrator> adminList;

	public List<Administrator> getAdminList() {
		return adminList;
	}
	public void setAdminList(List<Administrator> adminList) {
		this.adminList = adminList;
	}
	
	private String checkboxListDel;
	private long[]delAdminId=new long[]{};
	
	public long[] getDelAdminId() {
		return delAdminId;
	}
	public void setDelAdminId(long[] delAdminId) {
		this.delAdminId = delAdminId;
	}
	
	public String getCheckboxListDel() {
		return checkboxListDel;
	}
	public void setCheckboxListDel(String checkboxListDel) {
		this.checkboxListDel = checkboxListDel;
	}

	private int adminCount;
	
	public int getAdminCount() {
		return adminCount;
	}
	public void setAdminCount(int adminCount) {
		this.adminCount = adminCount;
	}
	
	
	public String insert_admin(){
		
		if(this.getAdmin()!=null){
			
			this.setAdmin(null);
		}
		
		return "admin_insert_success";
	}
	
	public String add_admin() throws Exception {
		
		Administrator a = new Administrator();
		a.setName(this.getAdmin().getName());
		a.setPassword(MD5.encoderByMd5(this.getAdmin().getPassword()));
		a.setAdminNote(this.getAdmin().getAdminNote());
		a.setEmail(this.getAdmin().getEmail());
		adminManager.add_admin(a);
		adminManager.add_adminPerm(a);
		return "admin_add_success";
	}
	
	public String upload_admin(){
		
		if(this.getAdmin().getAdminId()!=0){
			
			Administrator a = adminManager.findAdminByAdminId(this.getAdmin().getAdminId());
			System.out.println(a.getPassword());
			if(a!=null){
				this.setAdmin(a);
			}
		}
		
		return "admin_upload_success";
	}
	
	public String modify_admin() throws Exception{
		
		
		adminManager.modify_adminByAdmin(this.getAdmin());
		return "admin_modify_success";
	}
	public String del_admin() throws Exception{
		
		if(this.getAdmin().getAdminId()!=0){
			
			int adminId = this.getAdmin().getAdminId();
			Administrator a = adminManager.findAdminByAdminId(adminId);
			if(a!=null){
				adminManager.del_adminPermByAdminId(a.getAdminId());
				adminManager.del_adminByAdminId(a.getAdminId());
			}
		}
		
		return "admin_del_success";
	}
	public String del_adminList() throws Exception {
		
		String delIdList[] = this.getCheckboxListDel().split(", ");
		if(delIdList!=null&&delIdList.length>=1){
			for(int i=0;i<delIdList.length;i++){		
				adminManager.del_adminPermByAdminId(Integer.parseInt(delIdList[i]));
				adminManager.del_adminByAdminId(Integer.parseInt(delIdList[i]));
			}
		}
		
		return "adminList_del_success";
	}
	public String find_admin() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();
		Administrator a = (Administrator)ctx.getSession().get("admin");
		if(a!=null){
			
			this.setAdmin(a);
		}
		return "admin_find_success";	
	}
	
	
	public String find_adminList() throws Exception{
		
		adminCount = adminManager.getAdminCount();
		if(adminCount<1){
			
		}
		request = ServletActionContext.getRequest();
		int pageCount = adminManager.getPageCount(adminCount, adminManager.LIST_PAGE_SIZE);
		int pageNo;
		if(request.getParameter("pageNo")==null || request.getParameter("pageNo").trim().equals("")){
			pageNo=1;
		}try {
			
			pageNo=Integer.parseInt(request.getParameter("pageNo").trim());
		}catch (Exception e) {
			pageNo=1;
		}
		if(pageNo>pageCount){
			pageNo=pageCount;
		}
		this.setAdminList(adminManager.findAdminListAllByPage(pageNo));
		request.setAttribute("pageCount",pageCount);
		request.setAttribute("currentPage",pageNo);
		request.setAttribute("adminList",this.getAdminList());
		return "adminList_find_success";
	}
	
	

}
